import java.io.*;
import java.util.ArrayList;

import static java.lang.System.out;

public class GenerateOpeningDb {
    private static Console console;
    private static String openingFile = "opBook.cwo";

    public static void main(String[] args) {
        console = System.console();
        if (console == null) {
            throw new RuntimeException("Couldn't find a console to read input"
                                       + " from.");
        }

        ArrayList<String> lines = new ArrayList<String>();

        // 1st move for black, automatically
        Board init = new Board();
        lines.add(openingMoveString(init, new int[] {11, 15}));

        // 2nd move for black
         Board b1 = new Board(init);
         b1.makeMove(new int[] {11, 15});
         b1.blackToMove = !b1.blackToMove;
         ArrayList<Board> whitesFirst = b1.generateMoves();
         out.println("\nBlack's 2nd move:");
         ArrayList<Board> b2 = getMoves(lines, whitesFirst, "black");

        // 3rd move for black
//         out.println("\nBlack's 3rd move:");
//         for (Board move : b2) {
//             ArrayList<Board> whites2nd = move.generateMoves();
//             getMoves(lines, whites2nd, "black");
//         }

        // 1st move for white, automatically
        ArrayList<Board> blacksFirst = init.generateMoves();
        int[] wFirst = {22, 18};
        ArrayList<Board> w1 = new ArrayList<Board>();
        for (Board move : blacksFirst) {
            lines.add(openingMoveString(move, wFirst));
            Board next = new Board(move);
            next.makeMove(wFirst);
            next.blackToMove = !next.blackToMove;
            w1.add(next);
        }

        // 2nd move for white
        out.println("\nWhite's 2nd move:");
        for (Board move : w1) {
            ArrayList<Board> blacks2nd = move.generateMoves();
            ArrayList<Board> w2 = getMoves(lines, blacks2nd, "white");
        }

        // 3rd move for white
//         out.println("\nWhite's 3rd move:");
//         for (Board move : w2) {
//             ArrayList<Board> blacks3rd = move.generateMoves();
//             getMoves(lines, blacks3rd, "white");
//         }

        writeEm(lines);
    }

    public static void writeEm(ArrayList<String> lines) {
        printEm(lines);
        //Write a file
        try{
            Writer f = new BufferedWriter(new FileWriter(openingFile));
            for(String s : lines){
                f.write(s+"\n");
            }
            f.close();
        }catch (IOException e){
            out.println("Failed to write to file!!");
            //e.printStackTrace();
        }
    }

    public static ArrayList<Board> getMoves(ArrayList<String> lines,
                                         ArrayList<Board> moves, String color)
    {
        ArrayList<Board> queue = new ArrayList<Board>();
        for (Board move : moves) {
            out.println("\n" + color + " does:");
            GameEngine.printBoard(move);
            boolean isLegal = false;
            int[] response;
            do {
                response = getMove("black", 2);
                if (response == null) {
                    writeEm(lines);
                    System.exit(0);
                }

                isLegal = move.isLegalMove(response);
                if (!isLegal) {
                    out.println("That move isn't legal, type a new one");
                    out.println("\nWhite does:");
                    GameEngine.printBoard(move);
                }
            } while (!isLegal);
            lines.add(openingMoveString(move, response));
            Board next = new Board(move);
            next.makeMove(response);
            next.blackToMove = !next.blackToMove;
            queue.add(next);
        }

        return queue;
    }

    public static int[] getMove(String color, int moveNbr) {
        int[] move = null;
        while (move == null) {
            String  input = console.readLine("What is %s's move %d: ", color,
                                             moveNbr);

            if (input == null) {
                out.println();
                return null;
            }

            move = HumanPlayer.parseMove(input);

            if (move == null) {
                out.println("I'm sorry, but I don't understand what you are"
                            + " saying.");
                out.println("Please write your moves as: 11 - 15.");
                out.println("Valid squares are numbered 1 - 32.");
            }
        }

        return move;
    }

    public static void printEm(ArrayList<String> lines) {
        for (String line : lines) {
            out.println(line);
        }
    }

    public static String openingMoveString(Board board, int[] move) {
        StringBuilder s = new StringBuilder(100);
        s.append('<');
        board2Wdp(s, board);
        s.append("> <");
        s.append(move[0]);
        for (int i = 1; i < move.length; i++) {
            s.append('-');
            s.append(move[i]);
        }
        s.append('>');

        return s.toString();
    }

    public static void board2Wdp(StringBuilder s, Board board) {
        for (int row = 0; row < 8; row++) {
            int rowOffset = row * 4;

            for (int col = 0; col < 4; col++) {
                int bitNbr = rowOffset + col;
                char c = GameEngine.gamePiece(board, bitNbr);
                if(c == '-') c = '.';
                s.append(c);
            }
            s.append(' ');
        }
        s.append(board.blackToMove ? 'b' : 'w');
    }
}
